﻿using System;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
using Zove.ModelExplorer.Properties;
using Zove.Runtime.Core;
using Zove.Runtime.Core.Zoom;

namespace Zove.ModelExplorer
{
    public partial class Form1 : Form
    {
        private const int IMAGE_FOLDER = 0;

        private const int IMAGE_FOLDFOLDER = 1;

        private const int IMAGE_MODEL = 2;

        private const int IMAGE_VAR = 3;

        public Form1()
        {
            InitializeComponent();

            Load += Form1_Load;
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            var list = new ImageList();
            list.Images.Add(Resources.vm_folder);
            list.Images.Add(Resources.vm_folder_open);
            list.Images.Add(Resources.model);
            list.Images.Add(Resources.variable);

            tvModels.ImageList = list;

            string workspace = ConfigurationManager.AppSettings["workspace"];
            OpenWorkspace(workspace);
        }

        public void OpenWorkspace(string workspace)
        {
            var folder = new DirectoryInfo(workspace);

            tvModels.DoubleClick += tvModels_DoubleClick;

            TreeNode node = LoadObjects(folder);
            tvModels.Nodes.Clear();
            tvModels.Nodes.Add(node);
        }

        public void tvModels_DoubleClick(object sender, EventArgs e)
        {
            if (tvModels.SelectedNode.Tag is ZoomClass)
            {
                tbSource.Text = File.ReadAllText(tvModels.SelectedNode.ToolTipText);
            }
        }

        public TreeNode LoadObjects(DirectoryInfo folder)
        {
            var curr = new TreeNode(
                folder.Name, IMAGE_FOLDER, IMAGE_FOLDFOLDER);

            foreach (DirectoryInfo sd in folder.GetDirectories())
            {
                if (folder.Name == ".svn")
                {
                    continue;
                }

                TreeNode dn = LoadObjects(sd);

                curr.Nodes.Add(dn);
            }

            ZoomLanguageNature languageNature = new ZoomLanguageNature();

            foreach (FileInfo file in folder.GetFiles("*.zoom"))
            {
                ZoomClass zc = languageNature.ClassLoader.LoadFromFile(file.FullName);

                var mn = new TreeNode(zc.Name, IMAGE_MODEL, IMAGE_MODEL);
                mn.Tag = zc;
                mn.ToolTipText = file.FullName;
                curr.Nodes.Add(mn);

                foreach (string baseClass in zc.BaseClasses)
                {
                    var baseClassNode = new TreeNode(
                        baseClass, IMAGE_MODEL, IMAGE_MODEL);
                    baseClassNode.Tag = baseClass;
                    baseClassNode.ToolTipText = baseClass;
                    mn.Nodes.Add(baseClassNode);
                }

                foreach (ZoomVariable var in zc.Variables)
                {
                    var varNode = new TreeNode(var.Name, IMAGE_VAR, IMAGE_VAR);
                    varNode.Tag = var;
                    varNode.ToolTipText = var.Name;
                    mn.Nodes.Add(varNode);
                }
            }

            return curr;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var d = new FolderBrowserDialog();

            if (d.ShowDialog() == DialogResult.OK)
            {
                OpenWorkspace(d.SelectedPath);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var d = new AboutBox1();

            d.ShowDialog();
        }
    }
}